﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Obligatorio.DataAccess;

/// <summary>
/// Summary description for LoginPresenter
/// </summary>
public class LoginPresenter
{
    #region Fields
    ILoginView _view;
    #endregion


    public LoginPresenter(ILoginView view)
    {
        this._view = view;
    }

    public bool Login()
    {
        using (ObligatorioDotNetEntities data = new ObligatorioDotNetEntities())
        {
            var encryptedPW = Utils.GetMD5(this._view.password);
            var user = data.Users.Where(u => u.UserName == this._view.loginName &&
                                            u.UserPassword == encryptedPW).SingleOrDefault();

            if (user!= null)
            {
                CookieManager.SetValue(CommonValues.USER_ID_KEY, user.UserId.ToString());
                CookieManager.SetValue(CommonValues.USERNAME_KEY, user.UserName);
                return true;
            }

            return false;
        }
         
    }
}